/**************************************************************************
 *
 * Copyright (C) 2007 University of California, Los Angeles.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 *************************************************************************/

package edu.ucla.cs.typecast.util;

import java.io.*;
import java.util.*;

/**
 * Reads UNIX-style config files.
 */

public class ConfigurationReader {
	/**
	 * Config entries.
	 */
	
	protected Map<String, String> configs = new HashMap<String, String>();
	
	/**
	 * Value that get(key) returns when the corresponding value is not found.
	 */
	
	protected String defaultValue = null;

	/**
	 * Constructor. Reads the configuration from the given file.
	 * 
	 * @param str
	 * @throws IOException
	 */
	
	public ConfigurationReader(File file) throws IOException {
		this(new FileReader(file));
	}

	/**
	 * Constructor. Reads the configuration from the given reader.
	 * 
	 * @param str
	 * @throws IOException
	 */

	public ConfigurationReader(Reader rd) throws IOException {

		BufferedReader br = (rd instanceof BufferedReader) ? (BufferedReader) rd
				: new BufferedReader(rd);

		while (true) {
			String line = br.readLine();
			if (line == null)
				break;

			// Skip if the line is a comment (i.e. starts with space and then a
			// #)

			if (line.matches("^[\\s\\t]*#")) {
				continue;
			}

			String[] parts = line.split("[\\s\\t]*=[\\s\\t]*", 2);
			if (parts.length >= 2) {
				configs.put(parts[0], parts[1]);
			}
		}
	}

	/**
	 * Constructor. Reads the configuration from the given string.
	 * 
	 * @param str
	 * @throws IOException
	 */

	public ConfigurationReader(String str) throws IOException {
		this(new StringReader(str));
	}
	
	/**
	 * Get the value of a config entry.
	 * 
	 * @param key
	 * @return
	 */

	public String get(String key) {
		if (key != null && configs.containsKey(key))
			return configs.get(key);
		else
			return defaultValue;
	}
	
	public String getDefaultValue() {
		return defaultValue;
	}
	
	public void setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue;
	}

	public String toString() {
		String result = this.getClass().getName();
		result += "[";
		boolean first = true;
		for (String key : configs.keySet()) {
			result += key + "=" + configs.get(key);
			if (first) {
				result += ";";
				first = false;
			}
		}
		result += "]";

		return result;
	}
}
